import http from "../../../utils/http";

// pages/me_inner/update/update.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // 下拉选择
    selectSex: ['男生', '女生'],
    selectWish: ['减脂', '增肌', '均衡营养'],
    selectSport: ['轻量级活动', '中等强度活动', '高强度活动'],

    showSportDescription: false,

    // 个人身体数据
    sex: -1,
    height: undefined,
    weight: undefined,
    wish: -1,
    sport: -1,
  },

  onLoad: function (options) {
    this.getUserInfo();
  },

  bindPickerChange: function (e) {
    this.setData({
      wish: e.detail.value
    })
  },
  bindPickerChangeSport: function (e) {
    this.setData({
      sport: e.detail.value
    })
  },
  bindPickerChangeSex: function (e) {
    this.setData({
      sex: e.detail.value
    })
  },

  /**** 接口交互 ****/
  // 获取用户基本信息
  getUserInfo: function () {
    http({
      url: '/user/get',
      success: (result) => {
        console.log(result)
        this.setData({
          sex: result.info.sex,
          height: result.info.height,
          weight: result.info.weight,
          wish: result.info.wish,
          sport: result.info.sport
        })
        console.log(result);
      }
    })
  },

  showSprotDescription() {
    this.setData({
      showSportDescription: true
    })
  },

  /**** 表单交互 ****/

  bindUpdateOffline() {
    var that = this
    if (this.data.height == undefined) {
      wx.showModal({
        title: '提示',
        content: '请输入身高',
      })
      return
    }
    if (this.data.weight == undefined) {
      wx.showModal({
        title: '提示',
        content: '请输入体重',
      })
      return
    }
    if (this.data.wish == -1) {
      wx.showModal({
        title: '提示',
        content: '请输入饮食意向',
      })
      return
    }
    if (this.data.sport == -1) {
      wx.showModal({
        title: '提示',
        content: '请输入运动强度',
      })
      return
    }
    http({
      url: '/user/bindOffline',
      method: "put",
      data: {
        sex: that.data.sex,
        height: that.data.height,
        weight: that.data.weight,
        sport: that.data.sport,
        wish: that.data.wish
      },
      success: (result) => {
        wx.showToast({
          title: '修改成功',
          icon: '',
          duration: 500
        })
        this.getUserInfo();
      }
    })
  },

  // 其他输入绑定
  bindinput() {}

})